const fs = require("fs");
const { http } = require("../modal/db.config");

// 1.获取当前日期
const dest = __dirname + "/../public/upload/";
exports.upload = async (req, res) => {
  try {
    const originalname = req.file.originalname;
    const suffix = originalname.substring(originalname.lastIndexOf(".") + 1);
    const filename = req.file.filename + `.${suffix}`;
    const url = dest + filename;
    fs.renameSync(req.file.path, url);
    return res.json({
      code: 200,
      message: "success",
      data: http + "/upload/" + filename,
    });
  } catch (ex) {
    return res.json({ code: 200, message: "fail", data: ex.message });
  }
};
